/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Aptech.DAL;

import Aptech.DALInterface.LandsInterface;
import java.util.List;
import Aptech.DAL.MgLands;
import HibernateMappingPoJo.Lands;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author nkhoan
 */
public class MgLands implements LandsInterface {

    private SessionFactory sessFact;
    private Session sess;
    private Transaction tr;

    @Override
    public List<Lands> getAllLand() {
        List<Lands> list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(Lands.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }

        return list;
    }

    @Override
    public void addLands(Lands land) {
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();
            sess.save(land);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {

            System.out.println(he.getMessage());

        } finally {
            sess.close();

        }
    }

    @Override
    public void editLands(Lands land) {
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(land);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
    }

    @Override
    public void deleteLands(String id) {
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            String sql = "delete Lands where ID = '" + id + "'";
            sess.createSQLQuery(sql).executeUpdate();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
    }

    @Override
    public Lands getLandByID(String id) {
        Lands land = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(Lands.class).add(Restrictions.eq("id", id));
            land = (Lands) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return land;
    }
}
